/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cognitivetest;

import cognitivetest.toolLibs.DateTime;
import cognitivetest.toolLibs.MatlabControlInterface;
import cognitivetest.toolLibs.SubjectData;
import cognitivetest.toolLibs.SubjectDatabase;
import java.awt.CardLayout;
import java.awt.Frame;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Libra
 */
public class MainUI extends javax.swing.JFrame {

    /**
     * Creates new form MainUI
     */
    public MainUI() {
        initComponents();

        taskCards = (CardLayout) (pnlChooseTest.getLayout());
        parameterCards = (CardLayout) (pnlParameters.getLayout());
        taskCards.show(pnlChooseTest, "desc");
        parameterCards.show(pnlParameters, "desc");
        lblCurrentDate.setText(DateTime.getDateString());

        messageQueue = new LinkedBlockingQueue();
        startMessageWorker();
        message("Welcome!");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        demoRdoGrp = new javax.swing.ButtonGroup();
        pnlSubjectInfo = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnReadSubjectBase = new javax.swing.JButton();
        btnUseDemoSubjects = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        comboSubjects = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        lblCurrentDate = new javax.swing.JLabel();
        lblSex = new javax.swing.JLabel();
        lblAge = new javax.swing.JLabel();
        lblSid = new javax.swing.JLabel();
        pnlMajorTestClasses = new javax.swing.JPanel();
        btnLoadMatlab = new javax.swing.JButton();
        btnClassGenericCognitive = new javax.swing.JButton();
        btnClassAttention = new javax.swing.JButton();
        btnClassWorkingMemory = new javax.swing.JButton();
        btnClassFlexibility = new javax.swing.JButton();
        btnClassOthers = new javax.swing.JButton();
        btnTools = new javax.swing.JButton();
        pnlParameters = new javax.swing.JPanel();
        pnlParameterDesc = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        pnlStroopPara = new javax.swing.JPanel();
        btnStroopStart = new javax.swing.JButton();
        pnlVersionInfo = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pnlRunControl = new javax.swing.JPanel();
        pnlChooseTest = new javax.swing.JPanel();
        pnlTaskAttention = new javax.swing.JPanel();
        btnStroop = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        pnlTools = new javax.swing.JPanel();
        btnEditSubjectList = new javax.swing.JButton();
        btnExitSystem = new javax.swing.JButton();
        pnlTaskDesc = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("神经所-仁济医院 学习记忆");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pnlSubjectInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlSubjectInfo.setPreferredSize(new java.awt.Dimension(320, 280));

        jLabel4.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel4.setText("性别");

        jLabel5.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel5.setText("年龄");

        jLabel6.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel6.setText("编号");

        btnReadSubjectBase.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        btnReadSubjectBase.setText("读取被试数据集");

        btnUseDemoSubjects.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        btnUseDemoSubjects.setText("使用演示数据集");
        btnUseDemoSubjects.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUseDemoSubjectsActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel7.setText("选择被试");

        comboSubjects.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        comboSubjects.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboSubjectsActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel8.setText("测试日期");

        lblCurrentDate.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        lblCurrentDate.setText("0000/00/00");

        lblSex.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        lblSex.setText("     ");

        lblAge.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        lblAge.setText("     ");

        lblSid.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        lblSid.setText("     ");

        javax.swing.GroupLayout pnlSubjectInfoLayout = new javax.swing.GroupLayout(pnlSubjectInfo);
        pnlSubjectInfo.setLayout(pnlSubjectInfoLayout);
        pnlSubjectInfoLayout.setHorizontalGroup(
            pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboSubjects, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                        .addComponent(btnReadSubjectBase)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(btnUseDemoSubjects))
                    .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                        .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(lblSex))
                            .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(lblAge))
                            .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(lblSid))
                            .addComponent(jLabel7)
                            .addGroup(pnlSubjectInfoLayout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(lblCurrentDate)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnlSubjectInfoLayout.setVerticalGroup(
            pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlSubjectInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnReadSubjectBase)
                    .addComponent(btnUseDemoSubjects))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboSubjects, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblSex))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblAge))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(lblSid))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlSubjectInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(lblCurrentDate))
                .addContainerGap(57, Short.MAX_VALUE))
        );

        pnlMajorTestClasses.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlMajorTestClasses.setPreferredSize(new java.awt.Dimension(320, 320));
        pnlMajorTestClasses.setLayout(new java.awt.GridLayout(0, 1, 10, 2));

        btnLoadMatlab.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        btnLoadMatlab.setText("准备测试");
        btnLoadMatlab.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadMatlabActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnLoadMatlab);

        btnClassGenericCognitive.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnClassGenericCognitive.setText("普通认知");
        btnClassGenericCognitive.setEnabled(false);
        btnClassGenericCognitive.setPreferredSize(new java.awt.Dimension(120, 40));
        btnClassGenericCognitive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClassGenericCognitiveActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnClassGenericCognitive);

        btnClassAttention.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnClassAttention.setText("注意力");
        btnClassAttention.setEnabled(false);
        btnClassAttention.setPreferredSize(new java.awt.Dimension(120, 40));
        btnClassAttention.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClassAttentionActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnClassAttention);

        btnClassWorkingMemory.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnClassWorkingMemory.setText("工作记忆");
        btnClassWorkingMemory.setEnabled(false);
        btnClassWorkingMemory.setPreferredSize(new java.awt.Dimension(120, 40));
        btnClassWorkingMemory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClassWorkingMemoryActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnClassWorkingMemory);

        btnClassFlexibility.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnClassFlexibility.setText("行为可塑性");
        btnClassFlexibility.setEnabled(false);
        btnClassFlexibility.setPreferredSize(new java.awt.Dimension(120, 40));
        btnClassFlexibility.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClassFlexibilityActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnClassFlexibility);

        btnClassOthers.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnClassOthers.setText("其他");
        btnClassOthers.setEnabled(false);
        btnClassOthers.setPreferredSize(new java.awt.Dimension(120, 40));
        btnClassOthers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClassOthersActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnClassOthers);

        btnTools.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnTools.setText("工具");
        btnTools.setPreferredSize(new java.awt.Dimension(120, 40));
        btnTools.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolsActionPerformed(evt);
            }
        });
        pnlMajorTestClasses.add(btnTools);

        pnlParameters.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlParameters.setPreferredSize(new java.awt.Dimension(320, 360));
        pnlParameters.setLayout(new java.awt.CardLayout());

        jLabel3.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel3.setText("请选择测试类型和测试项目。");

        javax.swing.GroupLayout pnlParameterDescLayout = new javax.swing.GroupLayout(pnlParameterDesc);
        pnlParameterDesc.setLayout(pnlParameterDescLayout);
        pnlParameterDescLayout.setHorizontalGroup(
            pnlParameterDescLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlParameterDescLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(124, Short.MAX_VALUE))
        );
        pnlParameterDescLayout.setVerticalGroup(
            pnlParameterDescLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlParameterDescLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(326, Short.MAX_VALUE))
        );

        pnlParameters.add(pnlParameterDesc, "desc");

        pnlStroopPara.setPreferredSize(new java.awt.Dimension(316, 356));

        btnStroopStart.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 24)); // NOI18N
        btnStroopStart.setText("开始");
        btnStroopStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStroopStartActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlStroopParaLayout = new javax.swing.GroupLayout(pnlStroopPara);
        pnlStroopPara.setLayout(pnlStroopParaLayout);
        pnlStroopParaLayout.setHorizontalGroup(
            pnlStroopParaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlStroopParaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnStroopStart, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlStroopParaLayout.setVerticalGroup(
            pnlStroopParaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlStroopParaLayout.createSequentialGroup()
                .addContainerGap(245, Short.MAX_VALUE)
                .addComponent(btnStroopStart, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pnlParameters.add(pnlStroopPara, "paraStroop");

        pnlVersionInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlVersionInfo.setPreferredSize(new java.awt.Dimension(900, 48));

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(660, 40));

        txtMessage.setBackground(new java.awt.Color(240, 240, 240));
        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        txtMessage.setPreferredSize(new java.awt.Dimension(660, 40));
        txtMessage.setRequestFocusEnabled(false);
        jScrollPane1.setViewportView(txtMessage);

        jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 12)); // NOI18N
        jLabel1.setText("神经元功能环路研究组 2014.1 Ver 0.1 ");

        jLabel2.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 12)); // NOI18N
        jLabel2.setText("中国科学院神经科学研究所");

        javax.swing.GroupLayout pnlVersionInfoLayout = new javax.swing.GroupLayout(pnlVersionInfo);
        pnlVersionInfo.setLayout(pnlVersionInfoLayout);
        pnlVersionInfoLayout.setHorizontalGroup(
            pnlVersionInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlVersionInfoLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(pnlVersionInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(102, 102, 102))
        );
        pnlVersionInfoLayout.setVerticalGroup(
            pnlVersionInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(pnlVersionInfoLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(pnlVersionInfoLayout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pnlRunControl.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlRunControl.setPreferredSize(new java.awt.Dimension(320, 240));

        javax.swing.GroupLayout pnlRunControlLayout = new javax.swing.GroupLayout(pnlRunControl);
        pnlRunControl.setLayout(pnlRunControlLayout);
        pnlRunControlLayout.setHorizontalGroup(
            pnlRunControlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        pnlRunControlLayout.setVerticalGroup(
            pnlRunControlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 236, Short.MAX_VALUE)
        );

        pnlChooseTest.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlChooseTest.setLayout(new java.awt.CardLayout());

        pnlTaskAttention.setLayout(new java.awt.GridLayout(0, 1, 10, 10));

        btnStroop.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        btnStroop.setText("Stroop测试");
        btnStroop.setPreferredSize(new java.awt.Dimension(120, 40));
        btnStroop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStroopActionPerformed(evt);
            }
        });
        pnlTaskAttention.add(btnStroop);

        jButton10.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jButton10.setText("记忆二");
        jButton10.setPreferredSize(new java.awt.Dimension(120, 40));
        pnlTaskAttention.add(jButton10);

        jButton13.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jButton13.setText("记忆二");
        jButton13.setPreferredSize(new java.awt.Dimension(120, 40));
        pnlTaskAttention.add(jButton13);

        pnlChooseTest.add(pnlTaskAttention, "taskDecision");

        jPanel3.setLayout(new java.awt.GridLayout(0, 1, 10, 10));

        jButton11.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jButton11.setText("注意一");
        jButton11.setPreferredSize(new java.awt.Dimension(120, 40));
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton11);

        jButton12.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jButton12.setText("注意二");
        jButton12.setPreferredSize(new java.awt.Dimension(120, 40));
        jPanel3.add(jButton12);

        jButton14.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jButton14.setText("注意三");
        jButton14.setPreferredSize(new java.awt.Dimension(120, 40));
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton14);

        jButton15.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jButton15.setText("注意四");
        jButton15.setPreferredSize(new java.awt.Dimension(120, 40));
        jPanel3.add(jButton15);

        pnlChooseTest.add(jPanel3, "p3");

        pnlTools.setLayout(new java.awt.GridLayout(0, 1, 0, 4));

        btnEditSubjectList.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnEditSubjectList.setText("编辑被试数据");
        btnEditSubjectList.setPreferredSize(new java.awt.Dimension(120, 40));
        btnEditSubjectList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditSubjectListActionPerformed(evt);
            }
        });
        pnlTools.add(btnEditSubjectList);

        btnExitSystem.setFont(new java.awt.Font("Microsoft YaHei", 0, 14)); // NOI18N
        btnExitSystem.setText("退出程序");
        btnExitSystem.setPreferredSize(new java.awt.Dimension(120, 40));
        btnExitSystem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitSystemActionPerformed(evt);
            }
        });
        pnlTools.add(btnExitSystem);

        pnlChooseTest.add(pnlTools, "tools");

        jLabel9.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 14)); // NOI18N
        jLabel9.setText("请点击“准备测试”并选择测试类型。");

        javax.swing.GroupLayout pnlTaskDescLayout = new javax.swing.GroupLayout(pnlTaskDesc);
        pnlTaskDesc.setLayout(pnlTaskDescLayout);
        pnlTaskDescLayout.setHorizontalGroup(
            pnlTaskDescLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTaskDescLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addContainerGap(109, Short.MAX_VALUE))
        );
        pnlTaskDescLayout.setVerticalGroup(
            pnlTaskDescLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTaskDescLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addContainerGap(572, Short.MAX_VALUE))
        );

        pnlChooseTest.add(pnlTaskDesc, "desc");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlVersionInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 995, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlSubjectInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pnlMajorTestClasses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlChooseTest, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlParameters, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlRunControl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlParameters, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlRunControl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlSubjectInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlMajorTestClasses, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(pnlChooseTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlVersionInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoadMatlabActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadMatlabActionPerformed
        message("Starting Matlab...");
        boolean initOK = cognitivetest.toolLibs.MatlabControlInterface.init(this);
        this.setVisible(true);
        this.toFront();
        if (initOK) {
            message("Matlab started.");
            btnClassGenericCognitive.setEnabled(true);
            btnClassAttention.setEnabled(true);
            btnClassWorkingMemory.setEnabled(true);
        } else {
            message("Cannot start Matlab!");
        }
    }//GEN-LAST:event_btnLoadMatlabActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton11ActionPerformed

    private void btnClassWorkingMemoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClassWorkingMemoryActionPerformed
        taskCards.show(pnlChooseTest, "p3");
        parameterCards.show(pnlParameters, "desc");
    }//GEN-LAST:event_btnClassWorkingMemoryActionPerformed

    private void btnClassGenericCognitiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClassGenericCognitiveActionPerformed
        taskCards.show(pnlChooseTest, "taskCal");
        parameterCards.show(pnlParameters, "desc");
    }//GEN-LAST:event_btnClassGenericCognitiveActionPerformed

    private void btnClassAttentionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClassAttentionActionPerformed
        taskCards.show(pnlChooseTest, "taskDecision");
        parameterCards.show(pnlParameters, "desc");
    }//GEN-LAST:event_btnClassAttentionActionPerformed

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton14ActionPerformed

    private void btnToolsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolsActionPerformed
        taskCards.show(pnlChooseTest, "tools");
    }//GEN-LAST:event_btnToolsActionPerformed

    private void btnExitSystemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitSystemActionPerformed
        matSend("exit");
        System.exit(0);
    }//GEN-LAST:event_btnExitSystemActionPerformed

    private void delayedPop(int time) {
        try {
            TimeUnit.SECONDS.sleep(time);
            this.setVisible(true);
            this.setState(NORMAL);
            this.toFront();
        } catch (InterruptedException ex) {
//            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
            message(ex.toString());
        }
    }

    private void btnEditSubjectListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditSubjectListActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEditSubjectListActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        matSend("exit");
    }//GEN-LAST:event_formWindowClosing

    private void btnClassFlexibilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClassFlexibilityActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnClassFlexibilityActionPerformed

    private void btnClassOthersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClassOthersActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnClassOthersActionPerformed

    private void btnUseDemoSubjectsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUseDemoSubjectsActionPerformed
        subjects = (new SubjectDatabase()).getDemoDatabase();
        updateSubjects(subjects);
    }//GEN-LAST:event_btnUseDemoSubjectsActionPerformed

    private void comboSubjectsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboSubjectsActionPerformed
        if (comboSubjects.getItemCount() > 0) {
            updateSubjectData(comboSubjects.getSelectedIndex());
        }
    }//GEN-LAST:event_comboSubjectsActionPerformed

    private void btnStroopStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStroopStartActionPerformed
        this.setState(Frame.ICONIFIED);
        //        int trialNum = Integer.parseInt(txtTrialNum.getText()) * 3;
        //        String trialNumString = Integer.toString(trialNum);
        matSend("cd I:\\tasks\\Stroop\\");
            matSend("Stroop(1,1)");
            delayedPop(3);
    }//GEN-LAST:event_btnStroopStartActionPerformed

    private void btnStroopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStroopActionPerformed
        parameterCards.show(pnlParameters, "paraStroop");
    }//GEN-LAST:event_btnStroopActionPerformed

    private void updateSubjectData(int i) {
        SubjectData s = subjects.get(i);
        lblSex.setText(s.isIsMale() ? "男" : "女");
        lblAge.setText(Byte.toString(s.getAge()));
        lblSid.setText(Integer.toString(s.getSid()));
    }

    private void matSend(String s) {
        message(cognitivetest.toolLibs.MatlabControlInterface.send(s));
    }

    private void startMessageWorker() {
        class UpdateMessageTask extends TimerTask {

            @Override
            public void run() {
                try {
                    txtMessage.append(messageQueue.take());
                } catch (InterruptedException ex) {
                    System.out.println(ex.toString());
                }
            }
        }
        (new Timer()).scheduleAtFixedRate((new UpdateMessageTask()), 250, 250);
    }

    private void message(String s) {
        if (!s.isEmpty()) {
            try {
                messageQueue.put(s + "\n");
            } catch (InterruptedException ex) {
//            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println(ex.toString());
            }
        }
    }

    private void updateSubjects(List<SubjectData> l) {
        comboSubjects.removeAllItems();
        for (int i = 0; i < l.size(); i++) {
            comboSubjects.insertItemAt(l.get(i).getName(), i);
        }
        comboSubjects.setSelectedIndex(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClassAttention;
    private javax.swing.JButton btnClassFlexibility;
    private javax.swing.JButton btnClassGenericCognitive;
    private javax.swing.JButton btnClassOthers;
    private javax.swing.JButton btnClassWorkingMemory;
    private javax.swing.JButton btnEditSubjectList;
    private javax.swing.JButton btnExitSystem;
    private javax.swing.JButton btnLoadMatlab;
    private javax.swing.JButton btnReadSubjectBase;
    private javax.swing.JButton btnStroop;
    private javax.swing.JButton btnStroopStart;
    private javax.swing.JButton btnTools;
    private javax.swing.JButton btnUseDemoSubjects;
    private javax.swing.JComboBox comboSubjects;
    private javax.swing.ButtonGroup demoRdoGrp;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAge;
    private javax.swing.JLabel lblCurrentDate;
    private javax.swing.JLabel lblSex;
    private javax.swing.JLabel lblSid;
    private javax.swing.JPanel pnlChooseTest;
    private javax.swing.JPanel pnlMajorTestClasses;
    private javax.swing.JPanel pnlParameterDesc;
    private javax.swing.JPanel pnlParameters;
    private javax.swing.JPanel pnlRunControl;
    private javax.swing.JPanel pnlStroopPara;
    private javax.swing.JPanel pnlSubjectInfo;
    private javax.swing.JPanel pnlTaskAttention;
    private javax.swing.JPanel pnlTaskDesc;
    private javax.swing.JPanel pnlTools;
    private javax.swing.JPanel pnlVersionInfo;
    private javax.swing.JTextArea txtMessage;
    // End of variables declaration//GEN-END:variables

//    final private MatlabControlInterface matlab = new MatlabControlInterface();
    final private CardLayout taskCards;
    final private CardLayout parameterCards;
    final private BlockingQueue<String> messageQueue;
    private List<SubjectData> subjects;
}
